package com.qnw.CardGroupManagement;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.graphics.*;
import android.net.*;
import android.os.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.qrcode.*;
import com.google.zxing.qrcode.encoder.*;
import com.qnw.CardGroupManagement.adapter.*;
import com.ygo.feihua.*;
import com.ygo.feihua.util.*;
import java.io.*;
import java.util.*;

import android.support.v7.widget.Toolbar;
import android.view.View.OnClickListener;
import com.ygo.feihua.R;
import com.qnw.CardGroupManagement.bean.*;


public class A4 extends AppCompatActivity
{
	private String ydkpath;
	private String name;
	private String zypath;
	private String[][] ydkss;		

	private TextView tx_zkzs;
	private TextView tx_ews;
	private TextView tx_fkzs;
	
	private RecyclerView kz_rec;
	private DeckMessageAdapter dmAdp;
	private Gongju gj;
	private GridView kz_main,kz_ex,kz_side;
	private LinearLayout kz_zl,kz_el,kz_sl;
	
	private List<DeckCard> deckCards;
	
	List<Card> data;
	List<Card> data1;
	List<Card> data2;
	
	private DialogUtils du;
	
	public final static int DECK_TYPE_LOCAL=0;
	public final static int DECK_TYPE_NETWORK=1;
	private int deckType;
	private String ydkMessage;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a4_layout);

		kz_main=(GridView) findViewById(R.id.kz_main);
		kz_ex=(GridView) findViewById(R.id.kz_ex);
		kz_side=(GridView) findViewById(R.id.kz_side);
		kz_zl=(LinearLayout) findViewById(R.id.kz_zl);
		kz_el=(LinearLayout) findViewById(R.id.kz_el);
		kz_sl=(LinearLayout) findViewById(R.id.kz_sl);
		kz_rec=(RecyclerView) findViewById(R.id.kz_rec);
		
		
		Toolbar tool=(Toolbar) findViewById(R.id.toolbar);
		gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
		
		deckCards=new ArrayList<DeckCard>();
		data=new ArrayList<Card>();
		data1=new ArrayList<Card>();
		data2=new ArrayList<Card>();
		dmAdp=new DeckMessageAdapter(this,deckCards);
		
		kz_rec.setLayoutManager(new GridLayoutManager(this,10));
		kz_rec.setAdapter(dmAdp);
		
		Intent intent =this. getIntent();
		String action = intent.getAction();

        Uri uri = intent.getData();
	
		if (uri != null) {

			deckType = DECK_TYPE_NETWORK;
			String params = uri.getQueryParameter("params");
			if (params == null || params.equals("")) {
				gj.show("卡组数据为空");
				finish();
			}

			try {
				params = WenBase64.urlBase64Decode(params, "utf-8");
			} catch (UnsupportedEncodingException e) {
				Log.e("解析错误", "错误" + e);
			}

			String[] ss=params.split("_");
			if (ss[0].indexOf("#ourygo") != -1) {
				String sx = "";
				for (int i=0;i < ss.length;i++) {
					if (i != 0 && i != 1) {
						if (i == ss.length - 1) {
							sx += ss[i];
						} else {
							sx += ss[i] + "\n";
						}

					}
				}

				ydkss = File_ydk.messageYdk(sx);	
				if (ydkss == null) {
					gj.show("卡组数据读取错误");
					finish();
				}

				name = ss[1].substring(1, ss[1].length());
			}

		} else {

			if (intent.ACTION_VIEW.equals(action)) {
				deckType = DECK_TYPE_LOCAL;
				String path1=intent.getData().getPath();
				ydkpath = path1;
				//split方法返回数组,元素是原String见/分开
				String abb[]=path1.split("/");
				String c=abb[abb.length - 1];
				//"."是转义字符,必须得加\\
				String cc[]=c.split("\\.");
				name = cc[0];

			} else {
				deckType = intent.getIntExtra("type", DECK_TYPE_LOCAL);
				name = intent.getStringExtra("name");
				switch (deckType) {
					case DECK_TYPE_LOCAL:
						ydkpath = intent.getStringExtra("path");
						ydkss = File_ydk.ydk(ydkpath);	
						break;
					case DECK_TYPE_NETWORK:
						ydkMessage = intent.getStringExtra("message");
						ydkss = File_ydk.messageYdk(ydkMessage);	
						break;
					default:
						ydkss = new String[3][0];
				}

			}
		}
		gj.cshToolbar(tool,name);
		
		tool.setOnMenuItemClickListener(onMenuItemClick);
		kz_main.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					Log.e("准备打开A5","准备打开");
					Intent i=new Intent(A4.this, A5.class);
					i.putExtra("km", data.get(p3).getId());
					startActivity(i);
					// TODO: Implement this method
				}
			});
			
			
		kz_ex.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					Intent i=new Intent(A4.this, A5.class);
					i.putExtra("km", data1.get(p3).getId());
					startActivity(i);
					// TODO: Implement this method
				}
			});
			
		kz_side.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					Intent i=new Intent(A4.this, A5.class);
					i.putExtra("km", data2.get(p3).getId());
					startActivity(i);
					// TODO: Implement this method
				}
			});
			zypath=SharedPreferenceUtil.getYgopath();
		
		tx_zkzs = (TextView)findViewById(R.id.a4_tx_zkzs);
		tx_ews = (TextView)findViewById(R.id.a4_tx_ews);
		tx_fkzs = (TextView)findViewById(R.id.a4_tx_fczs);
		
		String imagepath=SharedPreferenceUtil.getYgopath()+"pics/";
		
		
		
		
		for(String s:ydkss[0]){
			
			Card card=new Card();
			card.setId(s);
			deckCards.add(DeckCard.toDeckCard(card,DeckCard.MAIN));
		}
	
		kz_main.setAdapter(new AdapterKz(A4.this,data,kz_main));
		
		for(String s:ydkss[1]){
			Card card=new Card();	
			card.setId(s);
			deckCards.add(DeckCard.toDeckCard(card,DeckCard.EXTRA));
		}
		kz_ex.setAdapter(new AdapterKz(A4.this,data1,kz_ex));
		
		for(String s:ydkss[2]){
			Card card=new Card();
			card.setId(s);
			deckCards.add(DeckCard.toDeckCard(card,DeckCard.MAIN));
		}
		dmAdp.sx();
		kz_side.setAdapter(new AdapterKz(A4.this,data2,kz_side));
	
		tx_zkzs.setText(ydkss[0].length + "");
		tx_ews.setText(ydkss[1].length + "");
		tx_fkzs.setText(ydkss[2].length + "");
		
	
	}
	
	public Bitmap tp(Bitmap a,Bitmap b)
	{
		//去掉二维码边框
		int p1=0;
		while(a.getPixel(p1,p1)==Color.WHITE)p1++;
		if(p1!=0)
			a=Bitmap.createBitmap(a,p1,p1,a.getWidth()-p1*2-1,a.getHeight()-p1*2-1);
		//获取单位方块的边长
		int w=0;
		while(a.getPixel(w,w)==Color.BLACK)w++;
		//如果边长是偶数压缩图片,使边长变成奇数
		if(w%2==0){a=ScaleTo(a,a.getWidth()-a.getWidth()/w,a.getHeight()-a.getHeight()/w);w--;}
		Bitmap bit=Bitmap.createBitmap(a.getWidth(),a.getHeight(),Bitmap.Config.ARGB_8888);
		Canvas c=new Canvas(bit);
		Paint pa=new Paint();
		//将图片大小和二维码保持一致
		b=ScaleTo(b,a.getWidth(),a.getHeight());
		c.drawBitmap(b,0,0,pa);
		//绘制二维码信息
		for(int i=0;i<a.getHeight();i+=w)
		{
			for(int u=0;u<a.getWidth();u+=w)
			{
				pa.setColor(a.getPixel(u+w/2,i+w/2));
				c.drawPoint(u+w/2,i+w/2,pa);
			}
		}
		pa.setColor(Color.BLACK);
		dwk(c,new Rect(0,0,w*7,w*7),w);
		dwk(c,new Rect(a.getWidth()-w*7,0,a.getWidth(),w*7),w);
		dwk(c,new Rect(0,a.getHeight()-w*7+1,w*7,a.getHeight()),w);
		if(iscorrect(a,w))
			dwk(c,new Rect(a.getWidth()-w*9,a.getHeight()-w*9,a.getWidth()-w*4,a.getHeight()-w*4),w);
		return bit;
	}
	public boolean iscorrect(Bitmap bmp,int dw){
		//判断有没有校验模块
		int[][] map={{0,0,0,0,0},
			{0,1,1,1,0},
			{0,1,0,1,0},
			{0,1,1,1,0},
			{0,0,0,0,0}};
		int x=(int)(bmp.getWidth()-dw*8.5f),y=(int)(bmp.getHeight()-dw*8.5f);
		for(int i=0;i<map.length;i++){
			for(int u=0;u<map[i].length;u++){
				if((bmp.getPixel(x+u*dw,y+i*dw)==Color.BLACK?0:1)!=map[i][u])return false;
			}
		}
		return true;
	}
	
	public void dwk(Canvas c,Rect r,int w)
	{//绘制定位模块
		Paint pai=new Paint();
		for(int i=0;i<3;i++)
		{
			if(i%2==0)pai.setColor(Color.BLACK);
			else pai.setColor(Color.WHITE);
			c.drawRect(r,pai);
			r.top+=w;r.bottom-=w;
			r.left+=w;r.right-=w;
		}
	}
	
	
	public Bitmap ScaleTo(Bitmap bitmap,float na,float nb) {
		Matrix matrix = new Matrix();
		matrix.postScale(na/bitmap.getWidth(),nb/bitmap.getHeight()); 
		Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
		//resizeBmp=big(resizeBmp,1080f/MainActivity.fblx);
		return resizeBmp;
	}
	public Bitmap loadbitmap(String fi)
	{
		return BitmapFactory.decodeFile(fi);
	}
	
	private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(MenuItem p1)
		{
			switch(p1.getItemId()){
				case R.id.kz_download:
					final String sx;
					
					String aa="#ourygo\n#"+name+"\n";

					aa+="#main\n";
					for(Card c:data){
						aa += c.getId() +"\n";
					}
					aa+="#extra\n";
					for(Card c:data){
						aa += c.getId() +"\n";
					}
					aa+="!side\n";
					for(Card c:data){
						aa += c.getId() +"\n";
					}
					sx=aa;
					try
					{
						final File ff=new File(SharedPreferenceUtil.getYgopath()+"deck/"+name+".ydk");
						if(ff.exists()){
							View[] vv=du.dialogt(true,ff.toString(),"卡组已存在,是否覆盖?");
							Button b1,b2;
							b1=(Button) vv[0];
							b2=(Button) vv[1];
							
							b1.setText("取消");
							b2.setText("覆盖");
							b1.setOnClickListener(new OnClickListener(){

									@Override
									public void onClick(View p1)
									{
										du.dis();
										// TODO: Implement this method
									}
								});
							b2.setOnClickListener(new OnClickListener(){

									@Override
									public void onClick(View p1)
									{
										try{
										FileOutputStream fos=new FileOutputStream(ff);
										fos.write(sx.getBytes());
										gj.show("已下载到"+ff);
										ydkpath=ff.toString();
										deckType=DECK_TYPE_LOCAL;
										}catch (IOException e){
											gj.show("下载失败,原因为"+e);
										}
										du.dis();
										// TODO: Implement this method
									}
								});
							
						}else{
						FileOutputStream fos=new FileOutputStream(ff);
						fos.write(sx.getBytes());
						gj.show("已下载到"+ff);
						ydkpath=ff.toString();
						deckType=DECK_TYPE_LOCAL;
						}
					}
					catch (IOException e)
					{
						gj.show("下载失败,原因为"+e);
					}
					break;
				case R.id.kz_edit:
					if(gj.isygo()){
					Intent intent1=new Intent("ygomobile.intent.action.DECK");
					intent1.putExtra(Intent.EXTRA_TEXT, ydkpath);
					startActivity(intent1);	
					}
					break;
				case R.id.kz_copy:
					String sjkpath=null;
					sjkpath=SharedPreferenceUtil.getCdbpath();
					String s="------主卡组共" + ydkss[0].length + "张------" + getWzkb(sjkpath, ydkss[0]) + "\n------额外卡组共" + ydkss[1].length + "张------" + getWzkb(sjkpath, ydkss[1]) + "\n------副卡组共" + ydkss[2].length + "张------" + getWzkb(sjkpath, ydkss[2]);
					ClipboardManager 名字= (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					名字.setText(s);
					gj.show("已复制到剪贴板");
					break;
				case R.id.kz_del:
					
					View[] v4=du.dialogt(true,ydkpath,"确定要删除这个卡组嘛？");
					Button b41,b42;
					b41=(Button) v4[0];
					b42=(Button) v4[1];
					b41.setText("取消");
					b42.setText("删除");
					b41.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});
					b42.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{if(FileUtil.delFile(ydkpath)){
									gj.show("删除成功");
									finish();
								}else{
									gj.show("删除失败");
								}
								du.dis();
								// TODO: Implement this method
							}
						});
					
					
					
					break;
				case R.id.kz_fx:
					
					du.dialogj1(false,"","生成图片中,请稍等");
				
					new Thread(new Runnable(){

							@Override
							public void run()
							{
							//	final File f=new File(ydkpath);
								String message="#ourygo_#"+name+"_";
								
								message+="#main_";
								for(Card c:data){
									message += c.getId() +"_";
								}
								message+="#extra_";
								for(Card c:data1){
									message += c.getId() +"_";
								}
								message+="!side_";
								for(Card c:data2){
									message += c.getId() +"_";
								}
								
								//message=message.replaceAll(" ","-");
								//message=message.replaceAll("#",".");
								try
								{
									message ="http://ourygo.top/html/deck.html?message="+ WenBase64.urlBase64Code(message, "utf-8");
									Bitmap bt = null;
									Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
									hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
									
				
									hints.put(EncodeHintType.MARGIN, 1);
									
									try {  
										QRCodeWriter writer = new QRCodeWriter();  
										
										// MultiFormatWriter writer = new MultiFormatWriter();  
										BitMatrix matrix = writer.encode(message, BarcodeFormat.QR_CODE,kz_ex.getWidth(),kz_ex.getWidth() /* 500, 500*/,hints);  
										
										//bt=QRCodeEncoder.syncEncodeQRCode(message,kz_ex.getHeight());
										
										bt=bitMatrix2Bitmap(matrix);
										//bt = CodeUtils.createImage(message, kz_ex.getHeight(), kz_ex.getHeight(), null);
										
										
										
										
									} catch (Exception e) {  
										Log.e("生成二维码错误","错误为"+e);
									}  

									//二维码中心图标
									//Bitmap  bmp = BitmapFactory.decodeResource(A4.this.getResources(),R.drawable.ourygo);
									//bt= QRCodeEncoder.syncEncodeQRCode(aa,kz_zl.getWidth());//,Color.parseColor("#008cf9"),bmp);
									Message me=new Message();
									me.obj=bt;
									me.what=0;
									handle.sendMessage(me);
								}
								catch (UnsupportedEncodingException e)
								{
									Log.e("编码错误","编码错误"+e);
								}

									
								//Base64.url
								
								
								// TODO: Implement this method
							}
						}).start();
					
						break;
				case R.id.kz_ks:
					String sjjkpath=null;
					sjjkpath=SharedPreferenceUtil.getCdbpath();

					String wzkb=getWzkb(sjjkpath, ydkss[0]);
					Intent intent=new Intent(A4.this,B5.class);
					intent.putExtra("wzkb", wzkb);
					startActivity(intent);			
					break;
				case R.id.kz_dckt:
					gj.show("请选择导出路径");
					startActivityForResult(new Intent(A4.this,NewWjxz.class).putExtra("lx",NewWjxz.TYPE_PATH),0);
				
					break;
			}
			// TODO: Implement this method
			return true;
		}
	};

	
	public static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {  
	
        int w = matrix.getWidth();  
        int h = matrix.getHeight();  
        int[] rawData = new int[w * h];  
        for (int i = 0; i < w; i++) {  
            for (int j = 0; j < h; j++) {  
                int color = Color.WHITE;  
                if (matrix.get(i, j)) {  
                    color = Color.BLACK;  
                }  
                rawData[i + (j * w)] = color;  
            }  
        }  
        Bitmap bitmap = Bitmap.createBitmap(w, h,Bitmap.Config.RGB_565);  
        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);  
      
		return bitmap;	
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(resultCode){
			case NewWjxz.TYPE_PATH:

				final String fzhpath=data.getStringExtra("path");
				View[] vv=du.dialogj1(false,"","准备导出中");
				dialog_tv=(TextView) vv[0];
				new Thread(new Runnable(){

						@Override
						public void run()
						{
							Set<String> list=new ArraySet<String>();
							for(String[] gg:ydkss){
								for(String g:gg){
									list.add(g);
								}
							}

							int c=list.size();
							int f=0;
							for(String s:list){
								boolean b=false;
								String fzqpath=SharedPreferenceUtil.getYgopath()+"pics/"+s+".jpg";
								File ff=new File(fzqpath);
								if (ff.exists()) {
									try {
										FileUtil.copyFile(fzqpath, fzhpath, false);
										b = true;
									} catch (IOException e) {
										b = false;
									}

								} else {
									try {
										java.util.zip.ZipFile zip=new java.util.zip.ZipFile(SharedPreferenceUtil.getYgopath() + "pics.zip");
										if (zip != null) {
											java.util.zip.ZipEntry ze=zip.getEntry("pics/" + s + ".jpg");
											if (ze != null) {

												int bytesum = 0;
												int byteread = 0;
												InputStream inStream=zip.getInputStream(ze);
												FileOutputStream fs = new FileOutputStream(fzhpath + s + ".jpg");
												byte[] buffer = new byte[1024];
												while ((byteread = inStream.read(buffer)) != -1) {
													bytesum += byteread; // 字节数 文件大小
													fs.write(buffer, 0, byteread);
												}
												inStream.close(); 
												b = true;
											}
										}
									} catch (IOException e) {
										Log.e("错误原因", "原因为" + e);
										b = false;
									}
								}
								shte("正在导出"+s+".jpg");			
								if(b){
									f++;
								}

							}
							Message me=new Message();
							me.what=2;
							handle.sendMessage(me);
							
							Message me1=new Message();
							me1.what=3;
							me1.obj="共"+c+"张,\n导出成功"+f+"张,\n导出失败"+(c-f)+"张";
							handle.sendMessage(me1);
												
							// TODO: Implement this method
						}
					}).start();
				break;		
		}
		
	}
	
	
	
	
	TextView dialog_tv;
	
	void shte(String s){
		Message me=new Message();
		me.obj=s;
		me.what=1;
		handle.sendMessage(me);
	}
	
	Handler handle=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case 0:
					
					Bitmap bitmap;
					Bitmap bitmap3;
					Bitmap bitmap1,bitmap2;
					
					Bitmap bitmap4,bitmap5;
					kz_zl.setDrawingCacheEnabled(true);
					kz_zl.buildDrawingCache();  //启用DrawingCache并创建位图
					bitmap3 = Bitmap.createBitmap(kz_zl.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_zl.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
					
					if(data.size()!=0){
					kz_main.setDrawingCacheEnabled(true);
					kz_main.buildDrawingCache();  //启用DrawingCache并创建位图
					bitmap = Bitmap.createBitmap(kz_main.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_main.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能

					bitmap3=BitmapUtil.mergeBitmap_TB(bitmap3,bitmap,true);
					}
					
					kz_el.setDrawingCacheEnabled(true);
					kz_el.buildDrawingCache();  //启用DrawingCache并创建位图
					bitmap4 = Bitmap.createBitmap(kz_el.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_el.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
					
					if(data1.size()!=0){
					kz_ex.setDrawingCacheEnabled(true);
					kz_ex.buildDrawingCache();  //启用DrawingCache并创建位图
					bitmap1= Bitmap.createBitmap(kz_ex.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_ex.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
					kz_ex.setBackgroundColor(Color.WHITE);
				 	// bitmap1=BitmapUtil.mergeBitmap_LR((Bitmap) msg.obj,bitmap1,true);
					
					bitmap4=BitmapUtil.mergeBitmap_TB(bitmap4,bitmap1,true);
					}
					
					
					kz_sl.setDrawingCacheEnabled(true);
					kz_sl.buildDrawingCache();  //启用DrawingCache并创建位图
					bitmap5 = Bitmap.createBitmap(kz_sl.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_sl.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能

					
					if(data2.size()!=0){
					kz_side.setDrawingCacheEnabled(true);
					kz_side.buildDrawingCache();  //启用DrawingCache并创建位图
					 bitmap2 = Bitmap.createBitmap(kz_side.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
					kz_side.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能

					bitmap5=BitmapUtil.mergeBitmap_TB(bitmap5,bitmap2,true);
					}
					
					
					Bitmap bitmap6=BitmapUtil.mergeBitmap_TB(bitmap3,
									BitmapUtil.mergeBitmap_TB(bitmap4,
									bitmap5,true),true);
							
				/*	Bitmap bit = Bitmap.createBitmap(bitmap6.getWidth()*2, bitmap6.getHeight()*2,Bitmap.Config.ARGB_8888);
					Canvas canvas = new Canvas(bit);
					//Rect topRect = new Rect(tempBitmapB.getWidth()-tempBitmapT.getWidth(),tempBitmapB.getHeight()-tempBitmapT.getHeight() ,bitmap.getWidth(),bitmap.getHeight());// tempBitmapB.getWidth(), tempBitmapB.getHeight());
					Rect bottomRect  = new Rect(0, 0, bitmap6.getWidth(), bitmap6.getHeight());

					Rect bottomRectT  = new Rect(0,0,bit.getWidth(),bit.getHeight());

					//  canvas.drawColor(Color.WHITE);
					canvas.drawBitmap(bitmap6, bottomRect, bottomRectT, null);
				//	canvas.drawBitmap(tempBitmapT, null, topRect, null);
					
						*/			
									
					Bitmap b7=BitmapUtil.mergeBitmap_TB(bitmap6,(Bitmap)msg.obj);//tp((Bitmap) msg.obj,bitmap6);//BitmapUtil.mergeBitmap_TB((Bitmap) msg.obj,bitmap6,true);																			   
					
					String strDlgTitle = "分享"+name;
					String strSubject="";
					String strContent="";
					// 图片文件路径（SD卡根目录下“1.png”图片）
					try {
						String imgPath = Gongju.saveBitmap(b7, Gongju.PATH_EZ_MANAGEMENT + "ydk_fenxiang/", name);
						// 图片URI
						Uri imageUri = Uri.fromFile(new File(imgPath));
						// 分享
						gj.ydkfx(strDlgTitle, strSubject, strContent, imageUri);
						
					} catch (IOException e) {
						gj.show("分享失败,原因为"+e);
					}
					
					du.dis();
					break;
			case 1:
					dialog_tv.setText((String)msg.obj);
					break;
			case 2:
				du.dis();
				break;
			case 3:
				View[] vv=du.dialogt1(true,"导出完毕",(String)msg.obj);
				Button bt=(Button) vv[0];
					bt.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});
				
				break;			
			}		
		}	
	};
	  
	public String getWzkb(String path, String[] kamis)
	{
		SQLiteDatabase sjk=SQLiteDatabase.openOrCreateDatabase(path, null);//数据库不存在则创建，存在则取得对象的实例
		Cursor texts=null;
		StringBuilder sb = new StringBuilder();
		for (int i=0;i < kamis.length;i++)
		{
			texts = sjk.query("texts", new String[]{"id","name"}, "id = ?", new String[]{kamis[i]}, null, null, null);
			if (texts.getCount() != 0)
			{//获取集合的个数，如果不为0(没查到则为0)
				texts.moveToNext();//移动到第一行，默认是0，没有写这句，直接获取值会抛异常
				if ("".equals(texts.getString(1)) || texts.getString(1) == null)
					sb.append("\n" + kamis[i]);
				else
				{
					sb.append("\n" + texts.getString(texts.getColumnIndex("name")));
				}
			}
			else
			{
				sb.append("\n" + kamis[i]);
			}
		}
		if (texts != null)
			texts.close();
		return sb.toString();
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method

		getMenuInflater().inflate(R.menu.menu_kazu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		switch(deckType){
			case DECK_TYPE_LOCAL:
				menu.findItem(R.id.kz_del).setVisible(true);  
				menu.findItem(R.id.kz_edit).setVisible(true);
				menu.findItem(R.id.kz_download).setVisible(false); 
				break;
			case DECK_TYPE_NETWORK:
				menu.findItem(R.id.kz_del).setVisible(false);  
				menu.findItem(R.id.kz_edit).setVisible(false);  
				menu.findItem(R.id.kz_download).setVisible(true);
				break;

		}	
		invalidateOptionsMenu();
		
		// TODO: Implement this method
		return super.onPrepareOptionsMenu(menu);
	}
	
	
	
	
}
